package com.dubito.gallery.ui.gallery

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.dubito.gallery.data.GalleryItem
import com.dubito.gallery.data.GalleryModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import org.koin.core.component.KoinComponent
import org.koin.core.component.get

data class ViewerUiState(
    val galleryItems: List<GalleryItem>
)

class ViewerViewModel : ViewModel(), KoinComponent {
    private val model: GalleryModel = get()

    val uiState: StateFlow<ViewerUiState> = model.galleryItemsFlow.map { galleryItems ->
        ViewerUiState(galleryItems = galleryItems)
    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = ViewerUiState(listOf())
    )
}